
import api from '@/api';
const user = {
    state: {
        token: null,
        refresh_token:null,
        userId:null,
        status:null,// 1激活 0未激活
        userTx:'',
        userName:'',
        labelArr:'',
        labelArr1:''
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_REFRESH_TOKEN: (state, refresh_token) => {
            state.refresh_token = refresh_token
        },
        SET_USERID: (state, userId) => {
            state.userId = userId
        },
        SET_STATUS: (state, status) => {
            state.status = status
        },
        SET_USERTX: (state, userTx) => {
            state.userTx = userTx
        },
        SET_USERNAME: (state, userName) => {
            state.userName = userName
        },
        SET_LABELARR: (state, labelArr) => {
            state.labelArr = labelArr
        },
        SET_LABELARR1: (state, labelArr1) => {
            state.labelArr1 = labelArr1
        },
    },

    actions: {
        // 登录
        userLogin({ commit }, params) {
            return new Promise((resolve, reject) => {
                console.log(params)
                api.user.weixin_mini_app_login(params).then(res=>{
                    console.log(res)
                    if(res.code == 200){
                        commit('SET_TOKEN', res.data.accessToken)
                        commit('SET_REFRESH_TOKEN', res.data.refreshToken)
                        commit('SET_USERID', res.data.userId)
                        commit('SET_STATUS',res.data.status)
                    }
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        //是否更新激活状态
        updateUserStatus({commit},state){
            return new Promise((resolve, reject) => {
                commit('SET_STATUS', state)
                resolve(true)
            })
        },

        //是否更新头像
        updateUserTx({commit},state){
            return new Promise((resolve, reject) => {
                commit('SET_USERTX', state)
                resolve(true)
            })
        },

        //是否更新用户名称
        updateUserName({commit},state){
            return new Promise((resolve, reject) => {
                commit('SET_USERNAME', state)
                resolve(true)
            })
        },

        //存资质数据
        updateUserZz({commit},state){
            return new Promise((resolve, reject) => {
                commit('SET_LABELARR', state)
                resolve(true)
            })
        },

        //存商标数据
        updateUserSb({commit},state){
            return new Promise((resolve, reject) => {
                commit('SET_LABELARR1', state)
                resolve(true)
            })
        },

        //是否刷新
        updateRefresh({commit},state){
            return new Promise((resolve, reject) => {
                commit('SET_REFRESH', state)
                resolve(true)
            })
        },
       
        logout ({ commit }) {
            return new Promise((resolve, reject) => {
            commit('SET_TOKEN', null)
            commit('SET_REFRESH_TOKEN', null)
            commit('SET_ISMEMBER',null)
            commit('SET_USERINFO', {})
            wx.clearStorageSync();
            resolve(true)
            })
            
          },
    }
}

export default user
